<?php

namespace App\Http\Controllers\Api;

use App\Models\Classify;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ClassifyController extends Controller
{
    public function lists(Request $request)
    {
        $where=[];
        if($request->has('level')){
            array_push($where,['level',$request->get('level')]);
        }
        if($request->has('classify_id')){
            array_push($where,['pa_id',$request->get('classify_id')]);
        }
        if(empty($where)){
            array_push($where,['level',1]);
        }
        $classify = Classify::where($where)
            ->select(['classify_id','name','img','level'])
            ->orderBy('sort','desc')
            ->get();
        foreach ($classify as $value){
            $value->my_img;
            if($value->pa_id==0){
                $value->children=Classify::where('pa_id',$value->classify_id)->get();
            }
        }
        $arr= $classify->toArray();
        array_push($arr,['name'=>"更多",'my_img'=>['pic_path'=>'../images/more.png'],'classify_id'=>0]);
        return ['status_code'=>1,'data'=>$arr];
    }
}
